package com.web.cms.doc.provider;

import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.doc.facade.DocFacade;
import com.web.cms.doc.message.DocRequest;
import com.web.cms.doc.message.DocResponse;
import com.web.cms.doc.model.Doc;
import com.web.cms.doc.service.DocService;
import com.web.cms.doc.util.DocConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("cmsProvider")
public class DocProvider implements DocFacade
{
	@Autowired
	private DocService docService;
	@Autowired
	private StorageClient storageClient;
	
	public DocProvider()
	{
	}

	@Override
	public DocResponse add(DocRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Doc doc = DocConvertor.toDoc(request);
		doc = docService.save(doc, request.getFileIds());
		DocResponse response = DocConvertor.toResponse(doc, storageClient);
		return response;
	}

	@Override
	public DocResponse update(DocRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Doc doc = DocConvertor.toDoc(request);
		doc = docService.save(doc, request.getFileIds());
		DocResponse response = DocConvertor.toResponse(doc, storageClient);
		return response;
	}

	@Override
	public DocResponse get(Integer id)
	{
		Doc doc = docService.get(id);
		DocResponse response = DocConvertor.toResponse(doc, storageClient);
		return response;
	}

	@Override
	public DocResponse getOne(Integer id)
	{
		Doc doc = docService.getOne(id);
		DocResponse response = DocConvertor.toResponse(doc, storageClient);
		return response;
	}

	@Override
	public Page<DocResponse> listPage(DocRequest request, int start, int size)
	{
		Doc doc = DocConvertor.toDoc(request);
		Page<Doc> page = new Page<>(size);
		page.setCurPage(start);
		page = docService.list(doc, page);
		Page<DocResponse> responsePage = DocConvertor.toResponse(page, storageClient);
		return responsePage;
	}
}
